﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DrawZoneCom;
using YArchitech.Controls;
using YArchitech.LIB;
using HYCodeBase.WinForm;
using HYLightingEquipmentLay.SmokeDetectorLayout;
using HYView;
using RegionSplittor.Utils;

namespace HYLightingEquipmentLay
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdNotBeamSmokeLayout : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			commandData.Application.Application.FailuresProcessing += DeleteWarning.OnFailuresProcessing;
			Result result = 0;
			try
			{
				if (SwitchView.SetUsableView(CmdViewType.FloorPlan, commandData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				Document document = commandData.Application.ActiveUIDocument.Document;
                Document linkedDocByCurDoc = RegionSplittor.Utils.LinkDocumentOperator.GetLinkedDocByCurDoc(document, "A");
                Document linkedDocByCurDoc2 = RegionSplittor.Utils.LinkDocumentOperator.GetLinkedDocByCurDoc(document, "S");
				if (linkedDocByCurDoc2 == null || linkedDocByCurDoc == null)
				{
					YJKMessageBox.Show("项目中不存在建筑或结构的链接文件！");
					return Autodesk.Revit.UI.Result.Failed;
				}
				List<CurveLoop> list = new List<CurveLoop>();
                foreach (List<CurveLoop> list2 in DrawZoneCom.DrawZoneCom.DrawZoneManual(commandData.Application, new HYCodeBase.WinForm.WindowHandle(Process.GetCurrentProcess().MainWindowHandle)))
				{
					if (list2.Count > 0)
					{
						list.Add(list2.First<CurveLoop>());
					}
				}
				if (list.Count > 0)
				{
					result = SmokeDetectorLayouter.NotBeamSmokeLayout(commandData, list, this._wndRevit);
				}
				else
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		private YJKWindowHandle _wndRevit;
	}
}
